package com.gamerole.app.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.aisier.network.observer.StateMutableLiveData
import com.gamerole.app.entity.ArticleBean
import com.gamerole.app.page.source.ArticlePagingSource
import com.gamerole.app.repository.ChatRepository
import com.gamerole.commom.base.BaseViewModel
import com.gamerole.commom.entity.PageBean
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ChatViewModel @Inject constructor(
    private var repository: ChatRepository
) : BaseViewModel() {

    val wxArticleLiveData = StateMutableLiveData<PageBean<ArticleBean>>()

    suspend fun requestNet(page: Int, type: Int) {
        wxArticleLiveData.value = repository.fetchWxArticleFromNet(page, type)
    }

    fun getPageData(type: Int): Flow<PagingData<ArticleBean>> =
        Pager(config = PagingConfig(pageSize = 10, prefetchDistance = 10 * 3),
            pagingSourceFactory = { repository.getArticlePagingSource(type) }).flow.cachedIn(
            viewModelScope
        )


}